package com.hengpeng.itfinoss.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.message.sequence.SequenceNoRequest;
import com.hengpeng.itfinbase.message.sequence.SequenceNoResponse;
import com.hengpeng.itfinoss.service.SequenceService;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * 流水号服务实现
 * </pre>
 * @author wangtongbin
 * @version 1.0, 2014-8-17
 */
@Service(SequenceService.CONTEXT_BEAN_NAME_SEQUENCESERVICE)
@Lazy
public class SequenceServiceImpl implements SequenceService
{
	@Autowired
	private ServiceFactory serviceFactory;
	
	@Override
	public String getSequenceNo(String prefix, String suffix, Integer length, String sequenceName, long sequenceCount,
			Long modMaxNum)
	{
		SequenceNoRequest seqRequest = new SequenceNoRequest();
		seqRequest.setPrefix(prefix);
		seqRequest.setSuffix(suffix);
		seqRequest.setSequenceName(sequenceName);
		seqRequest.setLength(length);
		seqRequest.setSequenceCount(sequenceCount);
		seqRequest.setModMaxNum(modMaxNum);
		seqRequest.setTransType(TransCodeConstants.OtherTransCode.SEQUENCE_NO);
		SequenceNoResponse seqResponse = serviceFactory.getRemoteService().remoteTrans(seqRequest, SequenceNoResponse.class);
		if (seqResponse.getRespCode().equals(RespCodeConstants.CommonCode.TRANS_SUC))
		{
			return seqResponse.getSequenceNo();
		}
		else
		{
			return null;
		}
	}

	@Override
	public long getSequence(String sequenceName)
	{
		return Long.valueOf(this.getSequenceNo(null, null, null, sequenceName, 1, null));
	}
}
